// pages/nearmap/nearmap.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isPerson: false,
    latitude: '',
    longitude: '',
    markers: [],
    lastX: 0,
    lastY: 0,
    shopInfo: '',
    submitData: {
      loginUserId: '', //	登录用户id	
      myLongitude: '', //	登录用户经度	
      myLatitude: '', //	登录用户纬度
      orderCondition: '', //	排序方式0：根据距离 1：根据人气 2：根据评价
      industry: '', //	行业类别	
      param: '',
      miles: '5000' //	附近多少米	
    },
    firup: 0,
    allTypes: [{ id: '', title: '全部分类' }, { id: 1, title: '美食' }, { id: 2, title: '酒店' }, { id: 3, title: '休闲娱乐' }, { id: 4, title: '丽人美发' }, { id: 5, title: '宠物' }, { id: 6, title: '母婴' }, { id: 7, title: '结婚摄影' }, { id: 8, title: '运动健身' }, { id: 9, title: '汽车服务' }, { id: 10, title: '家装建材' }, { id: 11, title: '教育培训' }, { id: 12, title: '服装鞋帽' }, { id: 13, title: '货运搬家' }, { id: 14, title: '家政' }, { id: 15, title: '更多' }],
    isFocus: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      location: wx.getStorageSync('location'),
      userInfo: JSON.parse(wx.getStorageSync('userInfo')),
      ['submitData.loginUserId']: JSON.parse(wx.getStorageSync('userInfo')).id,
      ['submitData.myLongitude']: wx.getStorageSync('location').longitude,
      ['submitData.myLatitude']: wx.getStorageSync('location').latitude,
      latitude: wx.getStorageSync('location').latitude,
      longitude: wx.getStorageSync('location').longitude,
    })
    this.getShopInfo();
  },
  touchmoveFunc: function (event){
    var currentX = event.touches[0].pageX
    var currentY = event.touches[0].pageY
    var tx = currentX - this.data.lastX
    var ty = currentY - this.data.lastY
    var text = ""
    if (Math.abs(tx) < Math.abs(ty)) {
      if (ty < 0){
        this.setData({
          firup: 1
        })
        if (currentY < 350){
          this.setData({
            firup: 2
          })
        }
      }
      else if (ty > 0 && this.data.firup > 0){
        this.setData({
          firup: 1
        })
        if (currentY > 450) {
          this.setData({
            firup: 0
          })
        }
      }
    }

    //将当前坐标进行保存以进行下一次计算
    this.data.lastX = currentX
    this.data.lastY = currentY
  },
  focusFunc: function () {
    this.setData({
      isFocus: true
    })
  },
  blurFunc: function (e) {
    if (!e.detail.value) {
      this.setData({
        isFocus: false
      })
    }
  },
  inputFunc: function (e) {
    this.setData({
      ['submitData.param']: e.detail.value,
      ['submitData.currentPage']: 1,
      shopInfo: [],
      hasData: true
    })
    if (!this.data.isPerson) {
      this.getShopInfo()
    }
    else {
      this.getPersonInfo()
    }

  },
  // 选择类型
  selectType: function(e){
    var id = e.currentTarget.dataset.id;
    this.setData({
      ['submitData.industry']: id
    })
    this.getShopInfo()
  },
  // 店铺列表
  getShopInfo: function () {
    var _this = this;
    wx.request({
      url: app.globalData.httpApiCar + 'nearbyInfo/getShopByPage',
      data: _this.data.submitData,
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        if (res.data.success) {
          var arr = [];
          var shop = res.data.data.pp;
          for (var i = 0; i < res.data.data.pp.length; i++) {
            shop[i].adress = shop[i].adress.split("区")[1];
            shop[i].distance = shop[i].distance.split(".")[0];
            var obj = { id: shop[i].id, latitude: shop[i].latitude, longitude: shop[i].longitude, width: '28', height: '32', iconPath: '/img/shop.png', label: { content: shop[i].shopName, x: -26, y:1, bgColor: '#fff', color: '#333', padding: 3, borderRadius:20 } }
            arr.push(obj)
          }
          _this.setData({
            shopInfo: shop,
            markers: arr
          })
        }
      }
    })
  },
  // 打开商家页面
  openIndex(e) {
    var item = e.currentTarget.dataset.item;
    if (!this.data.isPerson) {
      wx.navigateTo({
        url: "/pages/shopIndex/shopIndex?id=" + item.id
      })
    }
    else {
      wx.navigateTo({
        url: '/pages/personalProfile/personalProfile?id=' + item.userId
      })
    }
  },
  // 人列表
  getPersonInfo: function () {
    var _this = this;
    var item = _this.data.submitData;
    wx.request({
      url: app.globalData.httpApiCar + 'nearbyInfo/getUsers',
      data: { loginUserId: item.loginUserId, myLongitude: item.myLongitude, myLatitude: item.myLatitude, param: item.param, miles: 5000 },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        if (res.data.success) {
          var arr = [];
          var shop = res.data.data.pp;
          for (var i = 0; i < res.data.data.pp.length; i++) {
            shop[i].distance = shop[i].distance.split(".")[0];
            var obj = { id: shop[i].id, latitude: shop[i].latitude, longitude: shop[i].longitude, width: '28', height: '32', iconPath: '/img/person.png', label: { content: shop[i].shopName, x: -26, y: 1, bgColor: '#fff', color: '#333', padding: 3, borderRadius: 20 } }
            arr.push(obj)
          }
          _this.setData({
            shopInfo: shop,
            markers: arr
          })
        }
      }
    })
  },
  tabSwitch: function (e) {//tab页面切换
    var isPerson = e.target.dataset.value == "true" ? false : true;
    this.setData({
      isPerson: isPerson,
    })
    if (this.data.isPerson) {
      this.getPersonInfo()
    }
    else {
      this.getShopInfo()
    }
  },
  markertap: function(e){
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})